package xyz.test;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.graphics.drawable.DrawerArrowDrawable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;

import xyz.fragment.NoteBookListFragment;
import xyz.fragment.NoteListFragment;
import xyz.fragment.TagListFragment;

public class MainActivity extends AppCompatActivity {
    private Fragment[] mFragments;


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setContentView(R.layout.activity_main);

        FragmentManager mgr = getSupportFragmentManager();
        FragmentTransaction transaction = mgr.beginTransaction();
        transaction.add(R.id.frame, NoteListFragment.getNoteListFragment()).commit();

        //
        mFragments = new Fragment[3];

        mFragments[0] = NoteListFragment.getNoteListFragment();
        mFragments[1] = NoteBookListFragment.newInstance();
        mFragments[2] = TagListFragment.newInstance();

        TabLayout tl = findViewById(R.id.tabs);

        ViewPager viewPager = findViewById(R.id.view_paper);

        viewPager.setAdapter(new NoteFragmentPageAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT));

        tl.setupWithViewPager(viewPager);


        FloatingActionButton floatBtn = findViewById(R.id.float_btn);
        floatBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent it = new Intent(MainActivity.this, EditNoteActivity.class);
                it.putExtra("state", EditNoteActivity.STATE_NEW);

                startActivity(it);

            }
        });


        final Toolbar toolbar = findViewById(R.id.toolbar_main);

        DrawerArrowDrawable dad = new DrawerArrowDrawable(this);
        dad.setColor(getResources().getColor(android.R.color.white));
        if (toolbar != null) {

            toolbar.setNavigationIcon(dad);
            toolbar.inflateMenu(R.menu.toolbar_main_all);

            tl.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    switch (tab.getPosition()) {

                        case 0: {
                            toolbar.inflateMenu(R.menu.toolbar_main_all);
                            break;
                        }
                        case 1: {
                            toolbar.inflateMenu(R.menu.toolbar_main_notebook);
                            break;
                        }
                        case 2: {
                            toolbar.inflateMenu(R.menu.toolbar_main_tags);
                            break;
                        }


                    }
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    toolbar.getMenu().clear();
                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });

            toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.create: {
                            Toast.makeText(MainActivity.this, "撰写", Toast.LENGTH_SHORT).show();
                            break;
                        }
                        case R.id.search: {
                            Toast.makeText(MainActivity.this, "搜索", Toast.LENGTH_SHORT).show();
                            break;
                        }
                        case R.id.sync: {
                            Toast.makeText(MainActivity.this, "刷新", Toast.LENGTH_SHORT).show();
                            break;
                        }
                    }

                    return true;
                }
            });

        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.cs, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        return super.onOptionsItemSelected(item);
    }

    private class NoteFragmentPageAdapter extends FragmentPagerAdapter {

        private CharSequence[] mTabTitles = new CharSequence[]{
                "全部笔记", "笔记本", "标签"
        };

        public NoteFragmentPageAdapter(@NonNull FragmentManager fm, int behvior) {
            super(fm, behvior);
        }

        @NonNull
        @Override
        public Fragment getItem(int pos) {
            return mFragments[pos];
        }

        @Override
        public int getCount() {
            return mFragments.length;

        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return mTabTitles[position];
        }
    }
}